package com.haruhinav.controller;

import com.haruhinav.Utils.JWTUtils;
import com.haruhinav.entity.MusicServer;

import com.haruhinav.entity.PlayList;
import com.haruhinav.entity.UserPlaylist;
import com.haruhinav.service.MusicService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/music")
public class MusicController {

    @Resource
    private MusicService musicService;
// 所有歌单平台
    @RequestMapping(value = "/a/all-server",method = RequestMethod.GET)
    public List<MusicServer> getAllMusicServer(){
        return musicService.getAllMusicServer();
    }

// 所有歌单 一对多
    @RequestMapping(value = "/a/all-playlist_bysid",method = RequestMethod.GET)
    public Map<String,Object> getAllPlayListBySId(){

        return musicService.getAllPlayListBySId();
    }

//所搜歌单
    @GetMapping("/a/search-playlist")
    public List<PlayList> findPlaylistByExplainAndSId(@RequestParam("exp") String exp,@RequestParam("sid") Integer sid){
        PlayList playList = new PlayList();
        playList.setPlExplain(exp);
        playList.setServerId(sid);
       // return musicService.findPlaylistByExplainAndSId2(exp,sid);//mapper参数使用@param,mapper.xml不需要指定paramType
        return musicService.findPlaylistByExplainAndSId(playList);
    }

// 将用户还没有歌单的平台查询出来
    @GetMapping("/a/find-msevernotin")
    public List<MusicServer> findSeverNotInUserSid(@RequestParam("uid") Integer uid){
        return musicService.findMusicServerByUserNotInSId(uid);
    }
    // 获取用户的歌单
    @GetMapping("/a/find-playlistbyu")
    public List<MusicServer> findMServerByUNotInSId(@RequestParam("uid") Integer uid){
        System.out.println("--------"+uid);
        return musicService.findPlaylistByUser(uid);
    }

    //添加用户歌单或添加新的平台歌单
    @PostMapping("/a/add-playlist")
    public Map<String,Object> insertUserPlaylist(@RequestBody UserPlaylist userPlaylist,HttpServletRequest request){
        userPlaylist.setUserId(JWTUtils.fromTokenGetUid(request));
        return musicService.insertUserPlaylist(userPlaylist);
    }
    @PostMapping("/a/delupl-byid")
    public int delUserPlaylistById(@RequestBody UserPlaylist userPlaylist){
        return musicService.removePlaylistById(userPlaylist.getId());
    }
    @PostMapping("/a/delupl-byuidasid")
    public int delUserPlaylistByUIdAndSId(@RequestBody UserPlaylist userPlaylist,HttpServletRequest request){

        return musicService.removePlaylistByUIdAndSid(JWTUtils.fromTokenGetUid(request),userPlaylist.getServerId());
    }
//    根据id查询用户歌单
    @GetMapping("/a/find")
    public UserPlaylist findUPlaylistById(@RequestParam("id") int id){
        return musicService.findUserPlaylistById(id);
    }
//   修改
    @PostMapping("/a/update-upl")
    public Map<String,Object> updateUserPlaylistByid(@RequestBody UserPlaylist userPlaylist) {

        return musicService.updateUserPlaylistById(userPlaylist);
    }
//    只修改用户歌单备注（暂未启用）
    @PostMapping("/a/update-upl-exp")
    public int updateUPlExplainById(@RequestBody UserPlaylist userPlaylist){

        return musicService.updateUPlExplainById(userPlaylist.getPlExplain(),userPlaylist.getId());
    }
}
